//
//  NewFeatureCollectionViewController.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/9.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
import SnapKit
private let NewFeatureReuseIdentifier = "NewFeatureCell"
private let webImageCount = 4
class NewFeatureCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: NewFeatureReuseIdentifier)


    }
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        layout.itemSize = UIScreen.mainScreen().bounds.size
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return webImageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureReuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
         cell.imageIndex = indexPath.item
    
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        if page == (webImageCount-1){
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
              cell.showstartBtn()
        }
    }


}
private class NewFeatureCell: UICollectionViewCell{
    var imageIndex: Int = 0{
        didSet{
            imgView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            startBtn.hidden = true
        }
    }
    private lazy var imgView = UIImageView(frame: UIScreen.mainScreen().bounds)
    private lazy var startBtn = UIButton(title: "开始体验", backgroundImage: "new_feature_finish_button_highlighted", color: UIColor.whiteColor())
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func showstartBtn(){
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startBtn.transform=CGAffineTransformIdentity
            }) { (_) -> Void in
        }
        }
  @objc  private func startBtnClick(){
        print("点我了")
    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
   private    func setupUI(){
        addSubview(imgView)
        addSubview(startBtn)
        startBtn.sizeToFit()
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            startBtn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        }
        
    }
    
}

